package ma.sqli.vehicles;

import java.util.HashMap;
import java.util.Map;

public class VehicleFactory {

	private static Map<String, Vehicle> vehicles;

	public static Map<String, Vehicle> getVehicles( String contatenetedVehicles) 
	{
		vehicles = new HashMap<String, Vehicle>();
		String[] dividedVehicles = contatenetedVehicles.split(",");
		getVehiclesAndStoreItIntoMap(dividedVehicles);
		return vehicles;
	}

	private static void getVehiclesAndStoreItIntoMap( String[] dividedVehicles) 
	{
		Vehicle v = null;
		for (String vehicle : dividedVehicles) {
			String[] vehicleArguments = vehicle.split(":");
			String vehicleId = vehicleArguments[0].trim();
			String fuelType = vehicleArguments[1].trim();
			int numberOfDoors = Integer.parseInt(vehicleArguments[2].trim());

			v = getVehicleByFuelType(vehicleId, fuelType, numberOfDoors);
			vehicles.put(v.vehicleId, v);

		}

	}

	private static Vehicle getVehicleByFuelType(String vehicleId,String fuelType, int numberOfDoors) 
	{
		Vehicle v = null;
		if (fuelType.equals("Diesel"))
			v = new DieselVehicle(vehicleId, numberOfDoors);
		else if (fuelType.equals("Hybrid"))
			v = new HybridVehicle(vehicleId, numberOfDoors);
		else if (fuelType.equals("Gasoline"))
			v = new GasolineVehicle(vehicleId, numberOfDoors);
		return v;
	}

}
